const { regClass, property } = Laya;

@regClass()
export class CountDown extends Laya.Script {
    //declare owner : Laya.Sprite3D;
    declare owner: Laya.GImage;

    @property(Laya.GTextField)
    public labTime: Laya.GTextField;

    private down_time = 0;
    private downing = false;
    private tenLow = false;

    private call_back: Laya.Handler;

    //每帧更新时执行，尽量不要在这里写大循环逻辑或者使用getComponent方法
    onUpdate(): void {
        if (this.downing) {
            this.labTime.text = Math.floor(this.down_time / 1000).toString();
            this.down_time -= Laya.timer.delta;
            if (!this.tenLow && this.down_time <= 10000) {
                this.tenLow = true;
                Laya.SoundManager.playSound("resources/sound/countdown.mp3", 0);
            }
            if (this.down_time <= 0) {
                this.owner.visible = false;
                this.downing = false;
                this.tenLow = false;
                this.call_back && this.call_back.run();
                Laya.SoundManager.stopSound("resources/sound/countdown.mp3");
            }
        }
    }

    startDown(t: number, cb?: Laya.Handler) {
        if (t > 0) {
            this.down_time = t;
            this.downing = true;
            this.tenLow = false;
        } else {
            this.downing = false;
            this.tenLow = false;
            this.owner.visible = false;
        }
        this.call_back = cb;
    }
}